//
//  PoiFrameViewController.m
//  FootPrint
//
//  Created by 秦 东方 on 4/10/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "PoiFrameViewController.h"
#import "EventDetailViewController.h"

@interface PoiFrameViewController ()

@end

@implementation PoiFrameViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(_currentVC != _favVC || nil == _currentVC){
        
        if(nil == _favVC){
            _favVC = [[PoiFavViewController alloc] initWithNibName:@"PoiFavViewController" bundle:nil];
            _favVC.view.opaque = NO;
            _favVC.selDelegate = self;
        }
        [_currentVC.view removeFromSuperview];
        _currentVC = _favVC;
        [_contentView addSubview:_favVC.view];
        
        if(_mode == 0){
            [_favVC setMode:ModeManage];
        } else {
            [_favVC setMode:ModeChoose];
        }
    }
    _favVC.view.frame = CGRectMake(0, 0, _contentView.frame.size.width, _contentView.frame.size.height);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    _selDelegate = nil;
    [_btnBack release];

    [_contentView release];
    [_segment release];
    [super dealloc];
}
- (void)viewDidUnload {

    [self setBtnBack:nil];
    [self setContentView:nil];
    [self setSegment:nil];
    [super viewDidUnload];
}

- (void)setOpenMode:(PoiOpenMode)openMode{
    [_favVC setMode:openMode];
    [_searchVC setMode:openMode];
}

- (void)setMode:(int)mode{
    _mode = mode;
    if(mode == 0){
        [_favVC setMode:ModeManage];
        [_searchVC setMode:ModeManage];
    } else {
        [_favVC setMode:ModeChoose];
        [_searchVC setMode:ModeChoose];
    }
    
}

- (IBAction)segmentChange:(id)sender {
    if(_segment.selectedSegmentIndex == 0){
    
        if(_currentVC != _favVC || nil == _currentVC){
            
            if(nil == _favVC){
                _favVC = [[PoiFavViewController alloc] initWithNibName:@"PoiFavViewController" bundle:nil];
                _favVC.view.opaque = NO;
                _favVC.selDelegate = self;
            }
            [_currentVC.view removeFromSuperview];
            _currentVC = _favVC;
            [_contentView addSubview:_favVC.view];
            
            if(_mode == 0){
                [_favVC setMode:ModeManage];
            } else {
                [_favVC setMode:ModeChoose];
            }
        }
        _favVC.view.frame = CGRectMake(0, 0, _contentView.frame.size.width, _contentView.frame.size.height);

    }else{
    
        if(_currentVC != _searchVC || nil == _currentVC){
            
            if(nil == _searchVC){
                _searchVC = [[PoiSearchViewController alloc] initWithNibName:@"PoiSearchViewController" bundle:nil];
                _searchVC.view.opaque = NO;
                _searchVC.selDelegate = self;
            }
            [_currentVC.view removeFromSuperview];
            _currentVC = _searchVC;
            [_contentView addSubview:_searchVC.view];
        }
        _searchVC.view.frame = CGRectMake(0, 0, _contentView.frame.size.width, _contentView.frame.size.height);
        
        if(_mode == 0){
            [_searchVC setMode:ModeManage];
        } else {
            [_searchVC setMode:ModeChoose];
        }
    }
}

- (IBAction)back:(id)sender {
    
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
    [NVPopoverView slideOutPopoverView];
}

#pragma mark - POISelDelegate

- (void)didSelPOI:(MerchantMessage*)msg
{
    if (msg) {
        [_selDelegate didSelPOI:msg];
        [self.navigationController popToViewController:(EventDetailViewController*)_selDelegate animated:YES];
    }
}

@end
